package com.kbbywyclient.ui.activity;

import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.MinePartnerDetailsData;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.MinePartnerDetailsModule;
import com.kbbywyclient.presenter.MinePartnerDetailsPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.adapter.PartnerDetailsAdapter1;
import com.kbbywyclient.ui.adapter.PartnerDetailsAdapter2;
import com.kbbywyclient.ui.contarct.MinePartnerDetailsContract;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.widget.CustomToolBar;
import com.kbbywyclient.widget.divider.VerticalItemDecoration;

import java.util.Locale;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by Jiangzx on 2017/10/30 19:20.
 */

public class MinePartnerDetailsActivity extends BaseActivity implements MinePartnerDetailsContract.View{
    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;

    @BindView(R.id.app_bar)
    AppBarLayout appBar;

    @BindView(R.id.tv_partner_total)
    TextView tvPartnerTotal;
    @BindView(R.id.tv_partner_sep)
    TextView tvPartnerSep;

    @BindView(R.id.tv_total_application)
    TextView tvTotalApplication;
    @BindView(R.id.tv_total_settlement)
    TextView tvTotalSettlement;
    @BindView(R.id.ll_total_reward)
    LinearLayout llTotalReward;
    @BindView(R.id.tv_total_reward)
    TextView tvTotalReward;

    @BindView(R.id.tv_reward_with_unit)
    TextView tvRewardWithUnit;

    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;

    @Inject
    MinePartnerDetailsPresenter presenter;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_mine_partner_details;
    }

    @Override
    protected void initView() {
        toolBar.setLeftViewOnClickLinster(v->finish());

        int role=getUser().getRole();
        switch (role){
            case Constants.LEVEL_DISTRIBUTOR:
                //总代理
                llTotalReward.setVisibility(View.GONE);
                tvRewardWithUnit.setVisibility(View.GONE);
                break;
            case Constants.LEVEL_FIRST:
            case Constants.LEVEL_SECOND:
                //一级、二级合伙人
                tvPartnerSep.setVisibility(View.GONE);
                break;
            default:
                //普通合伙人
                tvPartnerSep.setVisibility(View.GONE);
                llTotalReward.setVisibility(View.GONE);
                tvRewardWithUnit.setVisibility(View.GONE);
                break;
        }

        final LinearLayoutManager layoutManager=
                new LinearLayoutManager(MinePartnerDetailsActivity.this,
                        LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        VerticalItemDecoration decoration=
                new VerticalItemDecoration(Constants.DIVIDER_COLOR,1);
        int padding= StringUtil.dip2px(15);
        decoration.setPaddingLeftRight(padding,padding);
        recyclerView.addItemDecoration(decoration);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState==RecyclerView.SCROLL_STATE_IDLE&&
                        layoutManager.findFirstCompletelyVisibleItemPosition()==0){
                    appBar.setExpanded(true,true);
                }
            }
        });
    }

    @Override
    public void initData() {
        presenter.loadPartnerDetails(getUser().getSalesID());
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getMinePartnerDetailsComponent(new MinePartnerDetailsModule(this)).inject(this);
    }

    @Override
    public void onPartnerDetailsSuccess(MinePartnerDetailsData data) {
        tvPartnerTotal.setText(data.getCount()+"个");
        tvTotalApplication.setText(String.valueOf(data.getAll_include()));
        tvTotalSettlement.setText(String.valueOf(data.getAll_settlement()));

        RecyclerView.Adapter adapter=null;

        int role=getUser().getRole();
        switch (role){
            case Constants.LEVEL_DISTRIBUTOR:
                //总代理
                tvPartnerSep.setText(data.getOne_level()+"个一级；"+data.getTwo_level()+"个二级");
                adapter=new PartnerDetailsAdapter1(data.getEvery_month());
                break;
            case Constants.LEVEL_FIRST:
            case Constants.LEVEL_SECOND:
                String totalReward=data.getAll_upper_prize_amount();
                if(TextUtils.isEmpty(totalReward)){
                    tvTotalReward.setText("0.00");
                }else{
                    tvTotalReward.setText(String.format(Locale.CHINESE,
                            Constants.AMOUNT_FORMAT,
                            Double.parseDouble(totalReward)));
                }
                adapter=new PartnerDetailsAdapter2(data.getEvery_month(),role);
                break;
            default:
                adapter=new PartnerDetailsAdapter2(data.getEvery_month(),role);
                break;
        }

        recyclerView.setAdapter(adapter);
    }
}
